#include "NewWhiteBoard.h"
#include "ui_NewWhiteBoard.h"

#include <QDebug>

NewWhiteBoard::NewWhiteBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NewWhiteBoard)
{
    ui->setupUi(this);

    init();
}

NewWhiteBoard::~NewWhiteBoard()
{
    delete ui;

    if(paintListWidget)
        delete paintListWidget;
    paintListWidget = nullptr;

    if(ploygonWidget)
        delete ploygonWidget;
    ploygonWidget = nullptr;

    if(sizePropertyWidget)
        delete sizePropertyWidget;
    sizePropertyWidget = nullptr;

    if(fontColorWidget)
        delete fontColorWidget;
    fontColorWidget = nullptr;

    if(backgroundColorWidget)
        delete backgroundColorWidget;
    backgroundColorWidget = nullptr;
}

void NewWhiteBoard::init()
{
    // 界面相关初始化
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowState(Qt::WindowMaximized);

    ui->PropertyWidget->setWindowOpacity(0.5);

    // 对象相关初始化
    ploygonWidget = new PloygonWidget(this);
    ploygonWidget->hide();

    sizePropertyWidget = new SizePropertyWidget(this);
    sizePropertyWidget->hide();

    fontColorWidget = new ColorWidget(COLORS::COLOR_White,this);
    fontColorWidget->hide();

    backgroundColorWidget = new ColorWidget(COLORS::COLOR_Black,this);
    backgroundColorWidget->hide();

    paintListWidget = new PaintListWidget(this);
    paintListWidget->hide();

    // 信号初始化
    connect(fontColorWidget,&ColorWidget::ColorChange,this,&NewWhiteBoard::setPen);
    connect(sizePropertyWidget,&SizePropertyWidget::SizeChange,this,&NewWhiteBoard::setPen);
    connect(ploygonWidget,&PloygonWidget::PloygonTypeChange,this,&NewWhiteBoard::setPloygonType);
    connect(backgroundColorWidget,&ColorWidget::ColorChange,this,&NewWhiteBoard::ChangeBackground);
    connect(paintListWidget,&PaintListWidget::DeleteDown,this,&NewWhiteBoard::ListDeleteDown);
    connect(paintListWidget,&PaintListWidget::OpenShow,this,&NewWhiteBoard::OpenShowPaint);
    connect(ui->paintWidget,&PaintWidget::changeBackgroundColor,[=](const QColor &bck){setPalette(bck);});
    connect(ui->paintWidget,&PaintWidget::LeftButtonPressed,[=](){AllPropertyWidgetHide();});
}

void NewWhiteBoard::on_Button_Pen_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->setModelType(PW::ModelType::FREELINE);
    ChangeButtonFace(PW::BUTTONFACE::_PEN);
}

void NewWhiteBoard::on_Button_Ploygon_clicked()
{
    bool IsHide = ploygonWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= ploygonWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_Ploygon->pos();
        ploygonWidget->setGeometry(PloygonFrameGeometry.x(),PloygonFrameGeometry.y()-PloygonSize.height()-30,PloygonSize.width(),PloygonSize.height());
        ploygonWidget->show();
    }
    else
        ploygonWidget->hide();

    ui->paintWidget->setModelType(PW::ModelType::PLOYGON);
    ChangeButtonFace(PW::BUTTONFACE::_PLOYGON);
}

void NewWhiteBoard::on_Button_Eraser_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->setModelType(PW::ModelType::ERASER);
    ChangeButtonFace(PW::BUTTONFACE::_ERASER);
}

void NewWhiteBoard::on_Button_PenSIze_clicked()
{
    bool IsHide = sizePropertyWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= sizePropertyWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PenSIze->pos();
        sizePropertyWidget->setGeometry(PloygonFrameGeometry.x(),PloygonFrameGeometry.y()-PloygonSize.height()-30,PloygonSize.width(),PloygonSize.height());
        sizePropertyWidget->show();
    }
    else
        sizePropertyWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_PENSIZE);
}

void NewWhiteBoard::on_Button_PenColor_clicked()
{
    bool IsHide = fontColorWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= fontColorWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PenColor->pos();
        fontColorWidget->setGeometry(PloygonFrameGeometry.x(),PloygonFrameGeometry.y()-PloygonSize.height()-30,PloygonSize.width(),PloygonSize.height());
        fontColorWidget->show();
    }
    else
        fontColorWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_LINECOLOR);
}

void NewWhiteBoard::on_Button_PaintWidgetBackground_clicked()
{
    bool IsHide = backgroundColorWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= backgroundColorWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PaintWidgetBackground->pos();
        backgroundColorWidget->setGeometry(PloygonFrameGeometry.x(),PloygonFrameGeometry.y()-PloygonSize.height()-30,PloygonSize.width(),PloygonSize.height());
        backgroundColorWidget->show();
    }
    else
        backgroundColorWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_BACKGROUNDCOLOR);
}

void NewWhiteBoard::on_Button_Revocation_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->RevocationSingleStep();
    ChangeButtonFace(PW::BUTTONFACE::_UNDOCURVE);
}

void NewWhiteBoard::on_Button_PaintWidgetClear_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->ClearPaint();
    ChangeButtonFace(PW::BUTTONFACE::_CLEARCURVE);
}

void NewWhiteBoard::on_Button_PaintWidgetList_clicked()
{
    bool IsHide = paintListWidget->isHidden();

    AllPropertyWidgetHide();

    if(IsHide)
    {
        QSize PloygonSize= paintListWidget->size();
        QPoint PloygonFrameGeometry = ui->PropertyWidget->pos() + ui->Button_PaintWidgetList->pos();
        paintListWidget->setGeometry(PloygonFrameGeometry.x(),PloygonFrameGeometry.y()-PloygonSize.height()-30,PloygonSize.width(),PloygonSize.height());

        // 对列表窗口操作
        paintListWidget->ListWidgetInit(ui->paintWidget->GetFilePtr()->FindQuote());

        paintListWidget->show();
    }
    else
        paintListWidget->hide();

    ChangeButtonFace(PW::BUTTONFACE::_PAINTLISE);
}

void NewWhiteBoard::on_Button_PaintWidgetSave_clicked()
{
    AllPropertyWidgetHide();

    ui->paintWidget->SaveData();
    ChangeButtonFace(PW::BUTTONFACE::_PAINTSAVE);
}

void NewWhiteBoard::on_Button_Exit_clicked()
{
    AllPropertyWidgetHide();
    ChangeButtonFace(PW::BUTTONFACE::_PAINTCLOSE);

    close();
}

void NewWhiteBoard::OpenShowPaint(const QString &tag)
{
    ui->paintWidget->ReadFileData(tag);
}

void NewWhiteBoard::ListDeleteDown(const QString &tag)
{
    ui->paintWidget->GetFilePtr()->DeleteSingleWidgetInfo(tag);

    paintListWidget->hide();
    on_Button_PaintWidgetList_clicked();
}

void NewWhiteBoard::ChangeBackground()
{
    QColor Color = backgroundColorWidget->GetColor();
    ui->paintWidget->setBackgroundColor(Color);
}

void NewWhiteBoard::AllPropertyWidgetHide()
{
    if(ploygonWidget && !ploygonWidget->isHidden())
        ploygonWidget->hide();

    if(sizePropertyWidget && !sizePropertyWidget->isHidden())
        sizePropertyWidget->hide();

    if(fontColorWidget && !fontColorWidget->isHidden())
        fontColorWidget->hide();

    if(backgroundColorWidget && !backgroundColorWidget->isHidden())
        backgroundColorWidget->hide();

    if(paintListWidget && !paintListWidget->isHidden())
        paintListWidget->hide();
}

void NewWhiteBoard::ChangeButtonFace(const PW::BUTTONFACE &bf)
{
    ui->Button_Pen->setStyleSheet("border-image: url(:/PropertyWidget/Resource/pen_1.png);");
    ui->Button_Ploygon->setStyleSheet("border-image: url(:/PropertyWidget/Resource/polygon_1.png);");
    ui->Button_Eraser->setStyleSheet("border-image: url(:/PropertyWidget/Resource/eraser_1.png);");
    ui->Button_PenSIze->setStyleSheet("border-image: url(:/PropertyWidget/Resource/size_1.png);");
    ui->Button_PenColor->setStyleSheet("border-image: url(:/PropertyWidget/Resource/color_1.png);");
    ui->Button_PaintWidgetBackground->setStyleSheet("border-image: url(:/PropertyWidget/Resource/Background_1.png);");
    ui->Button_Revocation->setStyleSheet("border-image: url(:/PropertyWidget/Resource/undo_1.png);");
    ui->Button_PaintWidgetClear->setStyleSheet("border-image: url(:/PropertyWidget/Resource/clearCurve_1.png);");
    ui->Button_PaintWidgetList->setStyleSheet("border-image: url(:/PropertyWidget/Resource/list_1.png);");
    ui->Button_PaintWidgetSave->setStyleSheet("border-image: url(:/PropertyWidget/Resource/save_1.png);");
    ui->Button_Exit->setStyleSheet("border-image: url(:/PropertyWidget/Resource/close_1.png);");

    if(bf == PW::BUTTONFACE::_PEN)
        ui->Button_Pen->setStyleSheet("border-image: url(:/PropertyWidget/Resource/pen_2.png);");
    else if(bf == PW::BUTTONFACE::_PLOYGON)
        ui->Button_Ploygon->setStyleSheet("border-image: url(:/PropertyWidget/Resource/ploygon_2.png);");
    else if(bf == PW::BUTTONFACE::_ERASER)
        ui->Button_Eraser->setStyleSheet("border-image: url(:/PropertyWidget/Resource/eraser_2.png);");
    else if(bf == PW::BUTTONFACE::_PENSIZE)
        ui->Button_PenSIze->setStyleSheet("border-image: url(:/PropertyWidget/Resource/size_2.png);");
    else if(bf == PW::BUTTONFACE::_LINECOLOR)
        ui->Button_PenColor->setStyleSheet("border-image: url(:/PropertyWidget/Resource/color_2.png);");
    else if(bf == PW::BUTTONFACE::_BACKGROUNDCOLOR)
        ui->Button_PaintWidgetBackground->setStyleSheet("border-image: url(:/PropertyWidget/Resource/Background_2.png);");
    else if(bf == PW::BUTTONFACE::_UNDOCURVE)
        ui->Button_Revocation->setStyleSheet("border-image: url(:/PropertyWidget/Resource/undo_2.png);");
    else if(bf == PW::BUTTONFACE::_CLEARCURVE)
        ui->Button_PaintWidgetClear->setStyleSheet("border-image: url(:/PropertyWidget/Resource/clearCurve_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTLISE)
        ui->Button_PaintWidgetList->setStyleSheet("border-image: url(:/PropertyWidget/Resource/list_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTSAVE)
        ui->Button_PaintWidgetSave->setStyleSheet("border-image: url(:/PropertyWidget/Resource/save_2.png);");
    else if(bf == PW::BUTTONFACE::_PAINTCLOSE)
        ui->Button_Exit->setStyleSheet("border-image: url(:/PropertyWidget/Resource/close_2.png);");
}

void NewWhiteBoard::setPen()
{
    QPen pen(fontColorWidget->GetColor(),sizePropertyWidget->GetSize());
    ui->paintWidget->setPen(pen);
}

void NewWhiteBoard::setPloygonType()
{
    ui->paintWidget->setPloygonType(ploygonWidget->GetPloygonType());
}
